package com.sun.hunk.util;


import com.sun.hunk.hibernate.domain.User;
import com.sun.hunk.properties.LogiwareProperties;
import javax.mail.MessagingException;

/**
 *
 * @author sunil 
 */
public class ErrorHandler {

    private User user;
    private String action;
    private Throwable e;

    public ErrorHandler(User user, String action, Throwable error) {
        this.user = user;
        this.action = action;
        this.e = error;
    }

    /**
     * Send report to logiware support through email.
     * @param e
     * @return
     * @throws MessagingException
     */
    public String reportError() throws MessagingException {
        String emails = LogiwareProperties.getProperty(LogiwareProperties.SUPPORT_TEAM);        
        String message = "<font color='blue' size='4'>Reported by : <b>" + user.getFirstName() + " " + user.getLastName() + "</b></font><br>"
                + "<font color='green' size='3'>Action : <b>" + action + "</b></font><br>"
                + "<font color='red' size='3'><b>" + e.toString() + "</b></font><br>"
                + "<pre>" + CommonUtil.getStackTrace(e) + "</pre>";
        return new EmailUtil().send(emails.split(","), " Error", message);
    }
}
